﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Common
{
    public class ConfigHelper
{
    // Methods
    public static string GetAppSetting(string key)
    {
        return GetAppSetting(key, true);
    }

    public static string GetAppSetting(string key, bool throwException)
    {
        try
        {
            return ConfigurationManager.AppSettings[key];
        }
        catch
        {
            if (throwException)
            {
                throw new Exception("没有在配置文件里找到名为'" + key + "'的配置信息。");
            }
            return "";
        }
    }

    public static bool GetAppSettingBoolean(string key)
    {
        return GetAppSettingBoolean(key, true);
    }

    public static bool GetAppSettingBoolean(string key, bool throwException)
    {
        try
        {
            return ConfigurationManager.AppSettings[key].ToLower().Equals("true");
        }
        catch
        {
            if (throwException)
            {
                throw new Exception("没有在配置文件里找到名为'" + key + "'的配置信息。");
            }
            return false;
        }
    }

    public static int GetAppSettingInteger(string key)
    {
        return GetAppSettingInteger(key, true);
    }

    public static int GetAppSettingInteger(string key, bool throwException)
    {
        try
        {
            string str = ConfigurationManager.AppSettings[key];
            return ConvertHelper.GetInteger(str);
        }
        catch
        {
            if (throwException)
            {
                throw new Exception("没有在配置文件里找到名为'" + key + "'的配置信息。");
            }
            return 0;
        }
    }

    public static string GetConnectionString(string key)
    {
        string connectionString;
        try
        {
            connectionString = ConfigurationManager.ConnectionStrings[key].ConnectionString;
        }
        catch
        {
            throw new Exception("没有在配置文件里找到名为'" + key + "'的连接字符串信息。");
        }
        return connectionString;
    }

    public static void LoadConfig(string configFilePath)
    {
        try
        {
            ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap {
                ExeConfigFilename = configFilePath
            };
            System.Configuration.Configuration configuration = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
            foreach (KeyValueConfigurationElement element in configuration.AppSettings.Settings)
            {
                ConfigurationManager.AppSettings[element.Key] = element.Value;
            }
            foreach (KeyValueConfigurationElement element2 in configuration.ConnectionStrings.ConnectionStrings)
            {
                ConfigurationManager.ConnectionStrings.Add(new ConnectionStringSettings(element2.Key, element2.Value));
            }
        }
        catch (Exception exception)
        {
            throw new Exception("config文件不存在或配置错误." + exception.Message);
        }
    }
}

}
